const { verifyToken } = require('./token')
const { passRouter } = require('../config')

const interception = async (req, res, next) => {
    try {
        if (passRouter.includes(req.baseUrl)) {
            next()
        } else {
            // 校验token
            const token = req.headers.imtoken
            if (!token) {
                return res.status(401).json({
                    code: 401,
                    msg: "授权参数异常"
                });
            } else {
                req.verifyToken = await verifyToken(token)
                next()
            }
        }
    } catch (error) {
        console.log('token err:', error)
        return res.status(401).json({
            code: -1,
            msg: "无效的token令牌"
        });
    }
}

module.exports = interception
